<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class KovStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'kovStatistics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'kov数据统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected $userTable = 'user';
    protected $frOrderTable = 'finance_recharge_order';
    protected $diamondStreamTable = 'finance_diamond_stream';
    protected $userRegisterLogTable = 'user_register_log';
    protected $userLoginLogTable = 'user_login_log';
    protected $kovStatisticsTable = 'kov_statistics';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $uuids = [

        ];

        do {
            $result = [];
            foreach($uuids as $uuid) {
                //获取用户信息
                $userInfo = DB::table($this->userTable)
                    ->where('uuid', $uuid)
                    ->select('uuid', 'user_id', 'create_timestamp')
                    ->first();
                $userInfo = $this->objToArray($userInfo);
                $userId = $userInfo['user_id'];
                //注册时间
                $register = $userInfo['create_timestamp'];
                //注册当日
                $registerDaySt = strtotime(date('Y-m-d 00:00:00', $userInfo['create_timestamp']));
                $registerDayEd = strtotime(date('Y-m-d 23:59:59', $userInfo['create_timestamp']));
                //注册三日后
                $registerDayEd3 = $registerDayEd + 86400*3;
                //注册七日后
                $registerDayEd7 = $registerDayEd + 86400*7;

                //总充值金额钻
                $rechargeAll = DB::table($this->frOrderTable)
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->selectRaw('user_id, sum(diamond_amount) as num')
                    ->first();
                $rechargeAll = $this->objToArray($rechargeAll);

                //总消费钻石
                $diamondAll = DB::table($this->diamondStreamTable)
                    ->where('user_id', $userId)
                    ->where('diamond_value', '<', 0)
                    ->selectRaw('user_id, sum(diamond_value) as num')
                    ->first();
                $diamondAll = $this->objToArray($diamondAll);

                //注册当日充值钻石
                $recharge1 = DB::table($this->frOrderTable)
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->where('pay_timestamp', '>=', $registerDaySt)
                    ->where('pay_timestamp', '<=', $registerDayEd)
                    ->selectRaw('user_id, sum(diamond_amount) as num')
                    ->first();
                $recharge1 = $this->objToArray($recharge1);

                //注册当日消费钻石
                $diamond1 = DB::table($this->diamondStreamTable)
                    ->where('user_id', $userId)
                    ->where('diamond_value', '<', 0)
                    ->where('change_timestamp', '>=', $registerDaySt)
                    ->where('change_timestamp', '<=', $registerDayEd)
                    ->selectRaw('user_id, sum(diamond_value) as num')
                    ->first();
                $diamond1 = $this->objToArray($diamond1);

                //注册后3日登陆次数
                $login3 = DB::table($this->userLoginLogTable)
                    ->where('user_id', $userId)
                    ->where('time', '>=', $registerDaySt)
                    ->where('time', '<=', $registerDayEd3)
                    ->selectRaw('user_id, count(1) as num')
                    ->first();
                $login3 = $this->objToArray($login3);

                //3日充值钻石
                $recharge3 = DB::table($this->frOrderTable)
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->where('pay_timestamp', '>=', $registerDaySt)
                    ->where('pay_timestamp', '<=', $registerDayEd3)
                    ->selectRaw('user_id, sum(diamond_amount) as num')
                    ->first();
                $recharge3 = $this->objToArray($recharge3);

                //3日消费钻石
                $diamond3 = DB::table($this->diamondStreamTable)
                    ->where('user_id', $userId)
                    ->where('diamond_value', '<', 0)
                    ->where('change_timestamp', '>=', $registerDaySt)
                    ->where('change_timestamp', '<=', $registerDayEd3)
                    ->selectRaw('user_id, sum(diamond_value) as num')
                    ->first();
                $diamond3 = $this->objToArray($diamond3);

                //7日登陆次数
                $login7 = DB::table($this->userLoginLogTable)
                    ->where('user_id', $userId)
                    ->where('time', '>=', $registerDaySt)
                    ->where('time', '<=', $registerDayEd7)
                    ->selectRaw('user_id, count(1) as num')
                    ->first();
                $login7 = $this->objToArray($login7);

                //7日充值钻石
                $recharge7 = DB::table($this->frOrderTable)
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->where('pay_timestamp', '>=', $registerDaySt)
                    ->where('pay_timestamp', '<=', $registerDayEd7)
                    ->selectRaw('user_id, sum(diamond_amount) as num')
                    ->first();
                $recharge7 = $this->objToArray($recharge7);

                //7日消费钻石
                $diamond7 = DB::table($this->diamondStreamTable)
                    ->where('user_id', $userId)
                    ->where('diamond_value', '<', 0)
                    ->where('change_timestamp', '>=', $registerDaySt)
                    ->where('change_timestamp', '<=', $registerDayEd7)
                    ->selectRaw('user_id, sum(diamond_value) as num')
                    ->first();
                $diamond7 = $this->objToArray($diamond7);

                $log = [
                    'uuid' => $uuid,
                    'userId' => $userId,
                    'registerDay' => date("Y-m-d H:i:s", $register),
                    'rechargeAll' => empty($rechargeAll['num']) ? 0 : $rechargeAll['num'],
                    'diamondAll' => empty($diamondAll['num']) ? 0 : abs($diamondAll['num']),
                    'recharge1' => empty($recharge1['num']) ? 0 : $recharge1['num'],
                    'diamond1' => empty($diamond1['num']) ? 0 : abs($diamond1['num']),
                    'login3' => empty($login3['num']) ? 0 : $login3['num'],
                    'recharge3' => empty($recharge3['num']) ? 0 : $recharge3['num'],
                    'diamond3' => empty($diamond3['num']) ? 0 : abs($diamond3['num']),
                    'login7' => empty($login7['num']) ? 0 : $login7['num'],
                    'recharge7' => empty($recharge7['num']) ? 0 : $recharge7['num'],
                    'diamond7' => empty($diamond7['num']) ? 0 : abs($diamond7['num']),
                ];
                $result[] = $log;
            }
            DB::table($this->kovStatisticsTable)->insert($result);

            print_r($result);

        } while (false);

    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

}